package com.simple.study.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.simple.study.base.service.impl.BaseServiceImpl;
import com.simple.study.entity.PageResult;
import com.simple.study.mapper.RoleMapper;
import com.simple.study.mapper.UserMapper;
import com.simple.study.pojo.Role;
import com.simple.study.pojo.User;
import com.simple.study.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

/**
 * @ClassName UserServiceImpl
 * @Author SimpleTree
 **/
@Service
public class UserServiceImpl extends BaseServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;

    public static final int ROLEID = 31;
    public static final String ROLENAME = "测试";

    @Override
    public UserMapper getMapper() {
        return userMapper;
    }

    /**
     * 登录验证
     * @param username
     * @return
     */
    @Override
    public User findByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    /**
     * 用户数据列表
     * @param query
     * @param pagenum
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<User> getUsersList(String query, Integer pagenum, Integer pagesize) {
        PageHelper.startPage(pagenum, pagesize);
        //PageHelper.clearPage();//清除PageHelper自动添加limit
        Page<User> page = userMapper.getUsersList(query, pagenum, pagesize);
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @Override
    public User addUser(User user) {
        user.setPassword(BCrypt.hashpw(user.getPassword(),BCrypt.gensalt()));
        user.setRoleId(ROLEID);
        user.setRoleName(ROLENAME);
        user.setMg_state(true);
        user.setType(1);
        user.setCreate_time(System.currentTimeMillis()+"");
        userMapper.addUser(user);
        return userMapper.selectByPrimaryKey(user.getUserId());
    }

    /**
     * 根据id查找用户
     * @param id
     * @return
     */
    @Override
    public User findById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据id修改用户
     * @param user
     * @param id
     * @return
     */
    @Override
    public User editUserById(User user, Integer id) {
        user.setUserId(id);
        userMapper.updateById(user);
        return userMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据id删除用户
     * @param userId
     */
    @Override
    public void deleteUserById(Integer userId) {
        userMapper.deleteByPrimaryKey(userId);
    }

    /**
     * 根据id修改用户状态
     * @param user
     * @param userId
     */
    @Override
    public void updateUserState(User user, Integer userId) {
        user.setUserId(userId);
        userMapper.updateUserState(user);
    }

    /**
     * 分配角色
     * @param userId
     * @param user
     */
    @Override
    public void distributionRole(Integer userId, User user) {
        user.setUserId(userId);
        Role role = roleMapper.selectByRoleName(user.getRoleId());
        user.setRoleName(role.getRoleName());

        userMapper.distributionRole(user);
    }
}
